from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
from time import sleep

class ASMS():
    def __init__(self,driver):
        self.driver=driver

    def login(self,taskId,loginName,password,vericode):
        self.driver.find_element(By.ID, 'taskId').clear()
        self.driver.find_element(By.ID, 'taskId').send_keys(taskId)
        self.driver.find_element(By.ID, 'loginName').clear()
        self.driver.find_element(By.ID, 'loginName').send_keys(loginName)
        self.driver.find_element(By.ID, 'password').clear()
        self.driver.find_element(By.ID, 'password').send_keys(password)
        self.driver.find_element(By.ID, 'vericode').clear()
        self.driver.find_element(By.ID, 'vericode').send_keys(vericode)
        sleep(1)
        self.driver.find_element(By.XPATH, '//*[@id="fmedit"]/div[2]/div[6]/input').click()
        sleep(2)

    def logout(self):
        self.driver.find_element(By.LINK_TEXT, '退出').click()
        sleep(1)

    def phone(self,phone):
        self.driver.find_element(By.ID, 'phone').clear()
        self.driver.find_element(By.ID, 'phone').send_keys(phone)
        self.driver.find_element(By.XPATH,'/html/body/div[2]/div/div[2]/div[2]/div[1]/form/table/tbody/tr[2]/td[3]/div/a').click()
        sleep(2)

    def password(self,):
        self.driver.find_element(By.XPATH, '/html/body/div[1]/div/div[1]/a').click()
        sleep(1)
        self.driver.find_element(By.ID, 'oldPassword').clear()
        self.driver.find_element(By.ID, 'oldPassword').send_keys('123456')
        sleep(1)
        self.driver.find_element(By.ID, 'newPassword1').clear()
        self.driver.find_element(By.ID, 'newPassword1').send_keys('123456')
        self.driver.find_element(By.ID, 'newPassword2').clear()
        self.driver.find_element(By.ID, 'newPassword2').send_keys('123456')
        sleep(1)
        self.driver.find_element(By.XPATH, '//*[@id="cboxLoadedContent"]/div/div/div[2]/div[4]/div[1]').click()
        sleep(1)


    def asset_add(self,title,providerId,storageId):
        self.driver.find_element(By.XPATH, '//*[@id="leftmenu_asset_manage"]/div[1]/a').click()
        sleep(1)
        self.driver.find_element(By.XPATH, '/html/body/div[2]/div/div[2]/div[2]/div[2]/div/input').click()
        self.driver.find_element(By.ID, 'title').clear()
        self.driver.find_element(By.ID, 'title').send_keys(title)
        ele = self.driver.find_element(By.ID, 'providerId')
        Select(ele).select_by_visible_text(providerId)
        ele = self.driver.find_element(By.ID, 'storageId')
        Select(ele).select_by_visible_text(storageId)
        sleep(1)
        self.driver.find_element(By.XPATH, '//*[@id="fm_new"]/div/div[1]/div/div[1]').click()
        sleep(1)
        self.driver.switch_to.alert.accept()
        sleep(2)

    def asset_modify(self,title):
        self.driver.find_element(By.XPATH, '/html/body/div[2]/div/div[2]/div[2]/div[2]/div/input').click()
        sleep(1)
        self.driver.find_element(By.XPATH, '/html/body/div[2]/div/div[2]/div[2]/div[3]/table/tbody/tr[2]/td[8]/a[1]').click()
        sleep(1)
        self.driver.find_element(By.ID, 'title').clear()
        self.driver.find_element(By.ID, 'title').send_keys(title)
        self.driver.find_element(By.XPATH, '//*[@id="fm_new"]/div/div[1]').click()
        sleep(2)

    def asset_search(self,title):
        self.driver.find_element(By.ID, 'title').clear()
        self.driver.find_element(By.ID, 'title').send_keys(title)
        self.driver.find_element(By.CLASS_NAME, 'search_button').click()